package vn.mahjonggame.game.event;

import java.util.Arrays;
import java.util.List;

import org.apache.log4j.Logger;

import vn.mahjonggame.game.Mahjong;
import vn.mahjonggame.game.MahjongExtension;
import vn.mahjonggame.game.request.UpdateRoomHandler;
import vn.mahjonggame.metadata.GameActionType;
import vn.mahjonggame.metadata.MObject;
import vn.mahjonggame.metadata.MatchState;
import vn.mahjonggame.metadata.Player;
import vn.mahjonggame.metadata.PlayerMode;
import vn.mahjonggame.metadata.RoomVarName;
import vn.mahjonggame.metadata.UserVarName;
import vn.mahjonggame.platform.PlatformClient;
import vn.mahjonggame.platform.PlatformConfig;
import vn.mahjonggame.util.Common;

import com.smartfoxserver.v2.core.ISFSEvent;
import com.smartfoxserver.v2.core.SFSEventParam;
import com.smartfoxserver.v2.entities.Room;
import com.smartfoxserver.v2.entities.User;
import com.smartfoxserver.v2.entities.data.ISFSArray;
import com.smartfoxserver.v2.entities.data.SFSArray;
import com.smartfoxserver.v2.entities.data.SFSObject;
import com.smartfoxserver.v2.entities.variables.RoomVariable;
import com.smartfoxserver.v2.entities.variables.SFSRoomVariable;
import com.smartfoxserver.v2.entities.variables.SFSUserVariable;
import com.smartfoxserver.v2.entities.variables.UserVariable;
import com.smartfoxserver.v2.exceptions.SFSException;
import com.smartfoxserver.v2.exceptions.SFSJoinRoomException;
import com.smartfoxserver.v2.extensions.BaseServerEventHandler;
import com.smartfoxserver.v2.extensions.ExtensionLogLevel;

public class OnUserJoinHandler extends BaseServerEventHandler {
		
	@Override
	public void handleServerEvent(ISFSEvent event) throws SFSException {
		// TODO Auto-generated method stub
		User user = (User) event.getParameter(SFSEventParam.USER);
		MahjongExtension ext = (MahjongExtension)getParentExtension();		
		Room room = ext.getGameRoom();
		List<User> users = room.getUserList();
		Mahjong mahjong = ext.getMahjong();
		Logger logger = mahjong.getLogger();
		
		/*
		 * Check the user balance
		 */
		
		if (ext.getMatchState() == MatchState.PLAYING 
				|| ext.getMatchState() == MatchState.SHOW_RESULT) {			
			
			logger.info(GameActionType.join.toString() + ": The game was started or completed.");
			return;			
		}
		
		if (ext.getPlayerMode() == PlayerMode.REAL_MODE.ordinal()) {			
			SFSObject msg = mahjong.gamePlayerJoin(user, GameActionType.join.toString());
			if(msg != null){
				send(GameActionType.join.toString(), msg, user);
				return;
			}
		}
		
		ext.addUserToMatch(user);
		
		UpdateRoomHandler updateRoomHandler = new UpdateRoomHandler();
		updateRoomHandler.updateRoom(ext, user, true);											
		logger.info(GameActionType.join.toString() + ": Send updateRoom cmd");
		
		SFSArray sfsUserArray = new SFSArray();		
		for(int i = 0; i < users.size(); i++){
			User iUser = users.get(i);
			int iUserId = iUser.getId();			
			Player iPlayer = mahjong.getPlayerById(iUserId);
			SFSObject oUser = new SFSObject();
			oUser.putUtfString(UserVarName.name.toString(), iUser.getName());
			oUser.putUtfString(UserVarName.avatar.toString(), iUser.getVariable(UserVarName.avatar.toString()).getStringValue());			
			
			sfsUserArray.addSFSObject(oUser);
			
			UserVariable positionVar = new SFSUserVariable(UserVarName.position.toString(), iPlayer.getPlayerPosition());			
			getApi().setUserVariables(iUser, Arrays.asList(positionVar));
		}
		
		RoomVariable usersVar = new SFSRoomVariable(MObject.users.toString(), sfsUserArray);
		usersVar.setOwner(null);
		usersVar.setGlobal(true);
       
        getApi().setRoomVariables(null, room, Arrays.asList(usersVar));	
		
		ext.addAction(user, GameActionType.join.toString());			
	}
}